$(function () {
  getInitCate();
  function getInitCate() {
    $.ajax({
      url: "/my/article/cates",
      success: function (res) {
        // console.log(res);
        if (res.status !== 0) {
          return layer.msg(res.message);
        }
        var htmlStr = template("tpl", res);
        $("tbody").html(htmlStr);
      },
    });
  }
  var layerIndex = null;
  $("#btnAdd").on("click", function () {
    // alert(1);
    layerIndex = layer.open({
      type: 1,
      title: "添加文章分类",
      area: ["500px", "250px"],
      content: $("#tplAdd").html(),
    });
  });
  //给表单绑定点击事件
  $("body").on("submit", "#form_cate", function (e) {
    e.preventDefault();
    $.ajax({
      method: "post",
      url: "/my/article/addcates",
      data: $(this).serialize(),
      success: function (res) {
        // console.log(res);
        if (res.status !== 0) {
          return layer.msg(res.message);
        }
        layer.msg("添加成功哈！");
        getInitCate();
        // 关闭弹出层
        layer.close(layerIndex);
      },
    });
  });
  //实现删除功能
  $("tbody").on("click", "#btnDel", function () {
    console.log(1);
    var id = $(this).attr("data-id");
    layer.confirm("确认删除?", { icon: 3, title: "提示" }, function (index) {
      //do something
      $.ajax({
        method: "get",
        url: "/my/article/deletecate/" + id,
        success: function (res) {
          if (res.status !== 0) {
            return layer.msg(res.message);
          }
          layer.msg("删除成功");
        },
      });
      layer.close(index);
    });
  });
});
